12. Solutions: Aggregates in Window Functions
Aggregates in Window Functions with and without ORDER BY
The ORDER BY
clause is one of two clauses integral to window functions. The ORDER
and PARTITION
define what is referred to as the “window”—the ordered subset of data over which calculations are made. Removing ORDER BY
just leaves an unordered partition; in our query's case, each column's value is simply an aggregation (e.g., sum, count, average, minimum, or maximum) of all the standard_qty
values in its respective account_id
.
As Stack Overflow user mathguy explains:
The easiest way to think about this - leaving the
ORDER BY
out is equivalent to "ordering" in a way that all rows in the partition are "equal" to each other. Indeed, you can get the same effect by explicitly adding theORDER BY
clause like this:ORDER BY 0
(or "order by" any constant expression), or even, more emphatically,ORDER BY NULL
.